package edu.xlh.travel.echart.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.xlh.travel.common.EchartInRedisConstant;
import edu.xlh.travel.redis.utils.JedisManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;

@Component
public class InitReportDataArrayInRedisUtil {
    @Autowired
    private JedisManager jedisManager;

    private static String writeKey = EchartInRedisConstant.ECHART_RD_DATA+"::"
            +EchartInRedisConstant.DATACOUNTWRITE_RD_LIST;
    private static String readKey = EchartInRedisConstant.ECHART_RD_DATA+"::"
            +EchartInRedisConstant.DATACOUNTREAD_RD_LIST;
    private static String  defaultMapStr = InitReportDataArrayInRedisUtil.getDefaultMap();
    public void getInitReportDataArrayOfWrite() {

        for (int i = 0; i < 7 ; i++) {
            jedisManager.rpush(writeKey , defaultMapStr);
        }
    }
    public void getInitReportDataArrayOfRead() {
        for (int i = 0; i < 7 ; i++) {
            jedisManager.rpush(readKey , defaultMapStr);
        }
    }
    public static String getDefaultMap() {
        HashMap<String, Integer> defaultHashMap = new HashMap<>();
        defaultHashMap.put("用户数量" , 0);
        defaultHashMap.put("订单数量" , 0);
        defaultHashMap.put("在线用户" , 0);
        ObjectMapper mapper = new ObjectMapper();
        String defaultStr = null;
        try {
            defaultStr = mapper.writeValueAsString(defaultHashMap);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return defaultStr;
    }
}
